package com.ebb;

import com.kinvey.KCSClient;
import com.kinvey.KinveyUser;
import com.kinvey.exception.KinveyException;
import com.kinvey.persistence.EntityDict;

import android.app.Activity;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class EBBSignUpActivity extends Activity implements OnClickListener {
	private EditText etId;
	private EditText etPwd;
	private EditText etNick;
	private Button btnSignUp;
	private Button btnBack;
	private String msgResponse;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.signup);
		
		initLayout();
	}
	
	public void initLayout() {
		this.etId = (EditText) findViewById(R.id.id_value);
		this.etPwd = (EditText) findViewById(R.id.pwd_value);
		this.etNick = (EditText) findViewById(R.id.nick_value);

		this.btnSignUp = (Button) findViewById(R.id.signup_button);
		this.btnBack = (Button) findViewById(R.id.back_button);
		this.btnSignUp.setId(0x2001);
		this.btnBack.setId(0x2002);
		this.btnSignUp.setOnClickListener(this);
		this.btnBack.setOnClickListener(this);
	}

	/**
	 * Display a simple alert dialog with the given text and title.
	 * 
	 * @param context
	 *            Android context in which the dialog should be displayed
	 * @param title
	 *            Alert dialog title
	 * @param text
	 *            Alert dialog message
	 */
	protected static void showAlert(Context context, String title, String text) {
		Builder alertBuilder = new Builder(context);
		alertBuilder.setTitle(title);
		alertBuilder.setMessage(text);
		alertBuilder.create().show();
	}

	public void onClick(View v) {
		KCSClient service = ((EBBKinveyApplication) getApplication()).getKinveyService();
		
		String idValue = etId.getText().toString();
		String pwdValue = etPwd.getText().toString();
		String nickValue = etNick.getText().toString();
		
		switch (v.getId()) {
		case 0x2001: // Sign Up
			try {
				if (idValue.length() > 0 && pwdValue.length() > 0 && nickValue.length() > 0) {
					service.userWithUsername(idValue, pwdValue);
					service.loginWithUsername(idValue, pwdValue);
					KinveyUser currentUser = service.getCurrentUser();
					currentUser.setAttribute("nickname", nickValue);
					msgResponse = "Success!";
					Toast.makeText(getBaseContext(), msgResponse, Toast.LENGTH_LONG).show();
					finish();
				} else {
					showAlert(EBBSignUpActivity.this, "EBB Sign Up", "Failure!");
				}
			} catch (KinveyException e) {
				showAlert(EBBSignUpActivity.this, "EBB Sign Up", "Failure!");
			}
			break;
		case 0x2002: // Back
			msgResponse = "Cancel!";
			Toast.makeText(getBaseContext(), msgResponse, Toast.LENGTH_LONG).show();
			finish();
			break;
		}
	}
}